package cn.itsource._05_topic;

import cn.itsource.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Rev2 {

    public static final String NAME_QUEUE2_TOPIC = "name_queue2_topic";

    public static void main(String[] args) throws Exception {

        //建立连接对象
        Connection connection = ConnectionUtil.getConnection();

        //建立通道
        Channel channel = connection.createChannel();

        //声明队列，队列名，持久，独占链接，队列用完删除，扩展参数
        channel.queueDeclare(NAME_QUEUE2_TOPIC,true,false,false,null);

        //绑定同一个交换机,指定delete队列
        channel.queueBind(NAME_QUEUE2_TOPIC, Send.NAME_EXCHANGE_TOPIC,"item.delete");
        channel.queueBind(NAME_QUEUE2_TOPIC, Send.NAME_EXCHANGE_TOPIC,"item.update");

        //同时处理一个消息
        channel.basicQos(1);

        //接受消息的回调方法
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //消息内容
                System.out.println("接受消息完成"+new String(body));
                //手动签收
                channel.basicAck(envelope.getDeliveryTag(),false);
                System.out.println("消费完成");
            }
        };
        //接受消息
        channel.basicConsume(NAME_QUEUE2_TOPIC, false, consumer);

        //自动接受，在接受数据时发生错误的话，可能会导致数据丢失
    }
}
